#version 460

#define M_PI 3.1415926535897932384626433832795
#define R_MAX 0.01

layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;

uniform int kernel_w, kernel_h;
uniform ivec2 index_kernel_iv;

shared int WorkGroupSize;
shared int index_kernel_row, index_kernel_col;
shared int index_kernel;
shared vec3 flag;

layout(rgba32f, binding = 0) uniform image2D radiance_map;
layout(rgba32f, binding = 1) uniform image2D kernel;
layout(rgba32f, binding = 2) uniform image2D radiance_map_after_sss;

void main() {
	int GlobalInvocationIndex = int(gl_WorkGroupID.x * gl_WorkGroupSize.x + gl_LocalInvocationID.x);
	if (GlobalInvocationIndex == 0)
	{
		WorkGroupSize = int(gl_WorkGroupSize.x * gl_WorkGroupSize.y * gl_WorkGroupSize.z);
		index_kernel_row = index_kernel_iv.y;
		index_kernel_col = index_kernel_iv.x;
		index_kernel = index_kernel_row * kernel_w + index_kernel_col;
		flag = imageLoad(kernel, ivec2(0, 0)).rgb;

		if (flag == vec3(0, 0, 1))
			return;

		vec3 sum = vec3(0);
		for (int row = 0; row < kernel_h; row++)
		{
			for (int col = 0; col < kernel_w; col++)
			{
				float weight = imageLoad(kernel, ivec2(col, row)).r;
				if (weight != 0)
				{
					vec3 radiance = imageLoad(radiance_map, ivec2(col, row)).rgb;
					sum += weight * radiance;
				}
			}
		}
		imageStore(radiance_map_after_sss, ivec2(index_kernel_col, index_kernel_row), vec4(sum, 1));
		// imageStore(radiance_map_after_sss, ivec2(index_kernel_col, index_kernel_row), vec4(1, 0, 0, 1));
	}
	barrier();
}
